<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <h2>bus总线</h2>
      <child></child>
      <child2></child2>
    </div>

    <script src="./lib/vue2.js"></script>
    <script>
      // 创建一个额外的组件来做组件通信
      const bus = new Vue();

      // bus.$emit(); // 提交
      // bus.$on(); // 做事件监听
      // bus.$off(); // 关闭事件监听

      const Child = {
        data() {
          return {
            count: 10,
          };
        },
        template: `
          <div>child1组件 - {{ count }} - <button @click="handleClick">btn</button></div>
        `,
        methods: {
          handleClick() {
            bus.$emit("change", this.count);
          },
        },
      };
      const Child2 = {
        data() {
          return {
            count: null,
          };
        },
        template: `
          <div>child2组件 - {{ count }}</div>
        `,
        mounted() {
          // 进入页面就开启事件监听
          bus.$on("change", (c) => {
            // console.log(c);
            this.count = c;
          });
        },
      };

      new Vue({
        components: {
          Child,
          Child2,
        },
      }).$mount("#app");
    </script>
  </body>
</html>
